<?php

//商品
namespace App\Http\Controllers\Admin\Mall;


use App\Enums\MallEnum;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ProductController extends BaseController
{
    //主模型
    private $model;

    public function __construct()
    {
        $this->model = app('model_mall_products');
    }

    /**
     * @api {Get} /mall/product/list 单品列表
     *
     * @apiGroup 个性商城-单品管理
     *
     * @apiParam {Int} is_rarity 是否稀有商品：-1（默认）：不限制；1：是；0：否
     * @apiParam {Int} is_recommend 是否推荐商品：-1（默认）：不限制；1：是；0：否
     * @apiParam {Int} cate_id 分类id，0：不限制，其它的根据接口
     * @apiParam {Int} sub_cate_id 子分类id，0：不限制，其它的根据接口
     * @apiParam {String} name 商品名称
     * @apiParam {Int} days 天数
     * @apiParam {Int} original_price 原始价格
     * @apiParam {Int} style_id 风格标签id，0：不限制，其它的根据接口
     * @apiParam {Int} status 出售状态：-1：不限制（默认）；1：上架；0：下架
     * @apiParam {String} add_date 添加日期，YYYY-mm-dd

     * @apiParam {String} page_size 每页显示条数，默认10
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": {
    "current_page": 338,
    "data": [
    {
    "id": 752, 序号
    "product_name": "规坐骑", 商品名称
    "category_id": 2, 分类id
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/47cdaf0a2844af04f3db6ec0e6eac8e1MWF5G.png", 图片
    "special": "", 特效
    "on_sale": 1, 销售状态：1：销售；2：下架
    "rarity": 0,
    "is_activity": 0,
    "add_time": "2021-10-27 10:38:20", 添加时间
    "discount_rate": 100, 折扣值，如果小于100则表示有打折
    "get_channel": "幸运树", 获得渠道
    "get_condition": "幸运树浇水第一名", 获得条件
    "activity_id": 0,
    "sub_cate_id": 3, 子分类id
    "is_buyable": 0, 是否可购买，1：是；0：否
    "rarity_tag_id": 2, 稀有度标签id
    "is_recommend": 0, 是否精选推荐，1：是；0：否
    "style": "可爱,梦幻,清新", 风格标签
    "status_name": "上架", 状态
    "rarity_tag_name": "稀有" 稀有度标签
    }
    ],
    "first_page_url": "http://qipaoadmin/mall/product/list?page=1",
    "from": 338,
    "last_page": 338,
    "last_page_url": "http://qipaoadmin/mall/product/list?page=338",
    "next_page_url": null,
    "path": "http://qipaoadmin/mall/product/list",
    "per_page": "1",
    "prev_page_url": "http://qipaoadmin/mall/product/list?page=337",
    "to": 338,
    "total": 338
    },
    "message": ""
    }
     */
    public function list(Request $request)
    {
        $param = $request->all();
        $v = app('validator')->make($param, [
            'is_rarity' => 'integer|in:-1,0,1',
            'is_recommend' => 'integer|in:-1,0,1',
            'cate_id' => 'integer|min:0',
            'sub_cate_id' => 'integer|min:0',
            'name' => 'string',
            'days' => 'integer|min:0',
            'original_price' => 'integer',
            'style_id' => 'integer|min:0',
            'status' => 'integer|in:-1,0,1',
            'add_date' => 'date_format:Y-m-d',
            'page_size' => 'integer|min:1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $list = app('repo_mall_products')->getList($param);
        return Response::sendData($list);
    }

    /**
     * @api {Get} /mall/discount-product/list 折扣商品列表
     *
     * @apiGroup 个性商城-单品管理
     *
     * @apiParam {Int} is_rarity 是否稀有商品：-1：不限制；1：是；0：否
     * @apiParam {Int} is_discount 是否折扣商品：-1：不限制；1：是；0：否
     * @apiParam {Int} cate_id 分类id，0：不限制，其它的根据接口，必须
     * @apiParam {Int} style_id 风格标签id，0：不限制，其它的根据接口，必须
     * @apiParam {Int} days 天数
     * @apiParam {String} name 商品名称
     * @apiParam {Int} status 出售状态：-1：不限制（默认）；1：上架；0：下架
     * @apiParam {String} add_date 添加日期，YYYY-mm-dd
     * @apiParam {Int} discount_rate 折扣率，0-100之间
     * @apiParam {Int} original_price 原始价格
     * @apiParam {String} page_size 每页显示条数，默认10
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": {
    "current_page": 1,
    "data": [
    {
    "id": 748, 商品id
    "product_name": "糖出鬼没", 商品名称
    "category_id": 1, 分类id
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/fcda779ef2606027cc4b9a3e3dac5c42C6O9m.png", 图片
    "special": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/77c49b027564f7c26638120746c9a745PW1Zl.webp", 特效
    "on_sale": 1, 出售状态，1：上架；2：下架
    "add_time": "2021-10-22 11:18:56",
    "discount_rate": 90, 折扣值
    "get_channel": "", 获得渠道
    "get_condition": "", 获得条件
    "sub_cate_id": 1, 子分类id
    "is_buyable": 1, 是否可以购买，1：是；0：否
    "rarity_tag_id": 0,
    "is_recommend": 0, 是否推荐
    "discount_status": 1, 折扣状态，1：开启；0：关闭
    "discount_sort": 2, 折扣排序号，升序
    "cate_name": "头像框", 分类名称
    "sub_cate_name": "气泡币专区", 子分类名称
    "style": "炫酷,搞怪", 风格标签
    "status_name": "上架", 出售状态名称
    "product_price": [ 商品价格：
    {
    "day": 3, 天数
    "original_price": 99999, 原价
    "price": 99999
    },
    {
    "day": 15,
    "original_price": 1,
    "price": 1
    }
    ],
    "rarity_tag_name": "" 稀有度标签名称
    }
    ],
    "first_page_url": "http://qipaoadmin/mall/discount-product/list?page=1",
    "from": 1,
    "last_page": 7,
    "last_page_url": "http://qipaoadmin/mall/discount-product/list?page=7",
    "next_page_url": "http://qipaoadmin/mall/discount-product/list?page=2",
    "path": "http://qipaoadmin/mall/discount-product/list",
    "per_page": "1",
    "prev_page_url": null,
    "to": 1,
    "total": 7
    },
    "message": ""
    }
     */
    public function discountList(Request $request)
    {
        $param = $request->only('page', 'page_size');
        $v = app('validator')->make($param, [
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:100',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $param['is_discount'] = 1;
        return Response::sendData(app('repo_mall_products')->getList($param));
    }

    /**
     * @api {Get} /mall/product/all 获取所有单品（下拉框）
     *
     * @apiGroup 个性商城-单品管理
     *
     * @apiParam {Int} top_cate_id 顶级分类id，所有单品：；活动商品：；套装：；稀有商品：；折扣商品：；
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": [
    {
    "product_id": 1, 商品id
    "product_name": "子爵", 商品名称
    "product_price": [ 商品价格信息：
    {
    "product_price_id": 3, 商品价格id
    "day": 7, 天数
    "price": 110 价格
    }
    ]
    },
    ]
    }
     */
    public function all(Request $request)
    {
        $topCateId = $request->top_cate_id;
        if (empty($topCateId)) {
            throw new BasicException(0, '顶级分类id不能为空');
        }

        $cates = app('model_mall_categories')->getSubCate($topCateId);
        $list = app('model_mall_products')->getProductList($cates);
        foreach ($list as &$v) {
            //商品天数和价格信息
            $v->product_price = app('model_mall_products_price')
                ->select('id as product_price_id', 'day', 'price')
                ->where('product_id', $v->product_id)
                ->get();
        }

        return Response::sendData($list);
    }

    /**
     * @api {post} /mall/product/add 添加商品
     *
     * @apiGroup 个性商城-单品管理
     *
     * @apiParam {Int} cate_id 分类id，必须
     * @apiParam {Int} sub_cate_id 子分类id，必须
     * @apiParam {String} product_name 商品名称，必须
     * @apiParam {String} product_desc 商品介绍，必须
     * @apiParam {String} picture 图片uri，必须
     * @apiParam {String} special 动效，非必须
     * @apiParam {String} show_type 展示位置：1：半屏；2：全屏；3：右侧，非必须
     * @apiParam {String} style_ids 风格标签id，多个用英文逗号连接，必须
     * @apiParam {String} is_buyable 是否可购买，1：是；0：否，必须
     * @apiParam {String} days_price 天数与价格，天数1:价格1;天数2:价格2...英文冒号和分号，当is_buyable=1时必须
     * @apiParam {String} get_channel 商品来源，当is_buyable=0时必须
     * @apiParam {String} get_condition 商品获取条件，当is_buyable=0时必须
     * @apiParam {Int} rarity_tag_id 稀有度标签id，0：无稀有度标签；其它根据接口
     * @apiParam {Int} sale_status 出售状态：1：上架；0：下架，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": []
    }
     */
    public function add(Request $request)
    {
        $param = $request->all();
        //参数校验
        $v = app('validator')->make($param, [
            'cate_id' => 'required|integer',
            'sub_cate_id' => 'required|integer',
            'product_name' => 'required|string|max:10',
            'product_desc' => 'string|max:100',
            'picture' => 'required|url',
//            'special' => 'required|url',
//            'show_type' => 'required|integer|in:1,2,3',
            'style_ids' => 'required|string',
            'is_buyable' => 'required|integer|in:1,0',
            'days_price' => 'required_if:is_buyable,1|string',
            'get_channel' => 'required_if:is_buyable,0|string|max:10',
            'get_condition' => 'required_if:is_buyable,0|string|max:20',
            'rarity_tag_id' => 'required|integer|min:0',
            'sale_status' => 'required|integer|in:0,1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $param['show_type'] = (!isset($param['show_type']) || !in_array($param['show_type'], [1, 2, 3])) ? 1 : $param['show_type'];

        return app('repo_mall_products')->addOrEdit($param);
    }

    /**
     * @api {get} /mall/product/detail 获取详情
     *
     * @apiGroup 个性商城-单品管理
     *
     * @apiParam {Int} id 商品id，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": {
    "id": 362, 序号
    "category_id": 2, 分类id
    "product_name": "赤兔", 商品名称
    "picture": "http://picture.jpg", 图片url
    "special": "http://special.jpg", 动效
    "show_type": 1, 展示位置：1：半屏；2：全屏；3：右侧
    "on_sale": 2, 销售状态：1：上架；2：下架
    "product_desc": "日行千里", 商品介绍
    "rarity": "1", 字段废弃
    "get_channel": "抢糖果", 商品来源，不可购买的商品有值
    "get_condition": "抢糖果月榜第一名获得", 获得条件，不可购买的商品有值
    "activity_id": 95, 活动id
    "discount_rate": 80, 折扣值
    "is_buyable": 1, 是否可以购买，1：是；0：否
    "style_ids": [ 风格id：
    2,
    4
    ],
    "product_price": [ 天数与价格信息：
    {
    "day": 7, 天数
    "price": 100 价格
    "original_price": 100 原始价格
    },
    {
    "day": 15,
    "price": 200
    "original_price": 220
    }
    ]
    }
    }
     */
    public function detail(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|integer',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $id = $request->id;

        $detail = $this->model
            ->select('id', 'category_id', 'title as product_name', 'picture', 'special', 'show_type','on_sale', 'desc as product_desc', 'rarity', 'get_channel', 'get_condition', 'activity_id', 'discount_rate', 'is_buyable', 'sub_cate_id', 'rarity_tag_id')
            ->where('id', $id)
            ->first();
        //查询风格
        $detail->style_ids = app('model_product_style_join')->where('product_id', $id)->pluck('style_id');
        //查询价格
        if ($detail->is_buyable == 1) {
            $detail->product_price = app('model_mall_products_price')
                ->select('day', 'price', 'original_price')
                ->where('product_id', $id)->get();
        }

        return Response::sendData($detail);
    }

    /**
     * @api {put} /mall/product/edit 编辑商品
     *
     * @apiGroup 个性商城-单品管理
     *
     * @apiParam {Int} id 商品id，必须
     * @apiParam {Int} cate_id 分类id，必须
     * @apiParam {Int} sub_cate_id 子分类id，必须
     * @apiParam {String} product_name 商品名称，必须
     * @apiParam {String} product_desc 商品介绍，必须
     * @apiParam {String} picture 图片uri，必须
     * @apiParam {String} special 动效，非必须
     * @apiParam {String} show_type 展示位置：1：半屏；2：全屏；3：右侧，非必须
     * @apiParam {String} style_ids 风格标签id，多个用英文逗号连接，必须
     * @apiParam {String} is_buyable 是否可购买，1：是；0：否，必须
     * @apiParam {String} days_price 天数与价格，天数1:价格1;天数2:价格2...英文冒号和分号，当is_buyable=1时必须
     * @apiParam {String} get_channel 商品来源，当is_buyable=0时必须
     * @apiParam {String} get_condition 商品获取条件，当is_buyable=0时必须
     * @apiParam {Int} rarity_tag_id 稀有度标签id，0：无标签；其它根据接口
     * @apiParam {Int} sale_status 出售状态：1：上架；0：下架，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": []
    }
     */
    public function edit(Request $request)
    {
        $param = $request->all();
        //参数校验
        $v = app('validator')->make($param, [
            'cate_id' => 'required|integer',
            'sub_cate_id' => 'required|integer',
            'product_name' => 'required|string|max:10',
            'product_desc' => 'string|max:100',
            'picture' => 'required|url',
//            'special' => 'required|url',
//            'show_type' => 'required|integer|in:1,2,3',
            'style_ids' => 'required|string',
            'is_buyable' => 'required|integer|in:1,0',
            'days_price' => 'required_if:is_buyable,1|string',
            'get_channel' => 'required_if:is_buyable,0|string|max:10',
            'get_condition' => 'required_if:is_buyable,0|string|max:20',
            'rarity_tag_id' => 'required|integer|min:0',
            'sale_status' => 'required|integer|in:0,1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $productPrice = $this->model->select('id')->where('id', $request->id)->first();
        if (empty($productPrice)) throw new BasicException(0, '商品不存在');

        $param['show_type'] = (!isset($param['show_type']) || !in_array($param['show_type'], [1, 2, 3])) ? 1 : $param['show_type'];

        return app('repo_mall_products')->addOrEdit($param);
    }

    /**
     * @api {delete} /mall/product/del 删除
     *
     * @apiGroup 个性商城-单品管理
     *
     * @apiParam {String} ids 分类id，多个用英文逗号连接，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": []
    }
     */
    public function del(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'ids' => 'required|string',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $ids = explode(',', $request->ids);

        DB::beginTransaction();
        try {
            foreach ($ids as $id) {
                $orderId = app('model_mall_orders')->where('product_id', $id)->value('id');
                if ($orderId) throw new BasicException(0, "有过订单的商品无法删除，商品id：{$id}");
            }
            //删除商品
            app('model_mall_products')->whereIn('id', $ids)->delete();
            //删除商品价格
            app('repo_mall_products_price')->delProductPrice($ids);
            //如果有已完成的心愿单不让删除
            $wish = app('model_mall_wishlist')->select('product_id')->whereIn('product_id', $ids)->where('status', 1)->first();
            if ($wish) {
                throw new BasicException(0, '商品有已完成的心愿单，无法删除，商品id：' . $wish->product_id);
            }
            //删除未完成的心愿单
            app('model_mall_wishlist')->whereIn('product_id', $ids)->where('status', 0)->delete();

            DB::commit();
            return Response::sendData([]);
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException(0, $e->getMessage());
        }
    }

    /**
     * @api {post} /mall/product/give 赠送
     *
     * @apiGroup 个性商城-单品管理
     *
     * @apiParam {number} id 产品主键，必传
     * @apiParam {number} user_code 用户编号，必传
     * @apiParam {number} use_days 使用天数，必传
     *
     * @apiSuccessExample 成功响应
     * {"code":200,"data":[]}
     */
    public function give(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|integer|min:1',
                'user_code' => 'required|integer|min:1',
                'use_days' => 'required|integer|min:1',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $param = $request->only(['id', 'user_code', 'use_days']);

        app('repo_mall_products')->give($param);

        return Response::sendData([]);
    }

    /**
     * @api {post} /mall/connect-product 添加人脉商品
     *
     * @apiGroup 人脉-商品
     *
     * @apiParam {Int} category_id 分类id，必须
     * @apiParam {String} product_name 商品名称，必须
     * @apiParam {String} picture 图片uri，必须
     * @apiParam {String} special 动效，非必须
     * @apiParam {String} price_info 价格信息，json格式，示例：{"day":7,"price":1000,"intimacy":30}，分别是天数，价格，亲密度
     * @apiParam {Int} sale_status 出售状态：1：上架；0：下架，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": []
    }
     */
    public function addConnect(Request $request)
    {
        $param = $request->only('category_id', 'product_name', 'picture', 'special', 'show_type', 'price_info', 'sale_status');
        //参数校验
        $v = app('validator')->make($param, [
                'category_id' => 'required|integer',
                'product_name' => 'required|string|max:10',
                'picture' => 'required|url',
                'special' => 'required',
                'price_info' => 'required|string',
                'sale_status' => 'required|integer|in:0,1',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $param['show_type'] = (!isset($param['show_type']) || !in_array($param['show_type'], [1, 2, 3])) ? 1 : $param['show_type'];

        app('repo_mall_products')->addOrEditConnect($param);
        return Response::sendData([]);
    }

    /**
     * @api {put} /mall/connect-product 编辑人脉商品
     *
     * @apiGroup 人脉-商品
     *
     * @apiParam {Int} id 商品id，必须
     * @apiParam {Int} category_id 分类id，必须
     * @apiParam {String} product_name 商品名称，必须
     * @apiParam {String} picture 图片uri，必须
     * @apiParam {String} special 动效，非必须
     * @apiParam {String} price_info 价格信息，json格式，示例：{"day":7,"price":1000,"intimacy":30}，分别是天数，价格，亲密度
     * @apiParam {Int} sale_status 出售状态：1：上架；0：下架，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": []
    }
     */
    public function editConnect(Request $request)
    {
        $param = $request->only('id', 'category_id', 'product_name', 'picture', 'special', 'show_type', 'price_info', 'sale_status');
        //参数校验
        $v = app('validator')->make($param, [
            'id' => 'required|integer|min:1',
            'category_id' => 'required|integer',
            'product_name' => 'required|string|max:10',
            'picture' => 'required|url',
            'special' => 'required',
            'price_info' => 'required|string',
            'sale_status' => 'required|integer|in:0,1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $param['show_type'] = (!isset($param['show_type']) || !in_array($param['show_type'], [1, 2, 3])) ? 1 : $param['show_type'];

        app('repo_mall_products')->addOrEditConnect($param);
        return Response::sendData([]);
    }

    /**
     * @api {get} /mall/connect-product 人脉商品列表
     *
     * @apiGroup 人脉-商品
     *
     * @apiParam {Int} category_id 分类id，0（默认）：不限制
     * @apiParam {String} product_name 商品名称
     * @apiParam {Int} price 价格
     * @apiParam {Int} intimacy 亲密值
     * @apiParam {Int} sale_status 出售状态：-1（默认）：不限制；1：上架；0：下架
     * @apiParam {String} add_time 添加时间，yyyy-mm-dd
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": {
    "current_page": 1,
    "data": [
    {
    "id": 642, 序号
    "category_id": 2,
    "product_name": "测试商品", 商品名称
    "day": 7, 天数
    "original_price": 1000, 价格
    "intimacy": 30, 亲密值
    "picture": "http://picture.jpg", 图片
    "special": "http://special.jpg", 特效
    "show_type": 2, 展示位置
    "on_sale": 1, 出售状态：1：上架；0：下架
    "add_time": "2021-08-09 10:43:16", 添加时间
    "category_name": "坐骑" 分类名称
    }
    ],
    "first_page_url": "http://local.qipaoadmin/mall/connect-product?page=1",
    "from": 1,
    "last_page": 1,
    "last_page_url": "http://local.qipaoadmin/mall/connect-product?page=1",
    "next_page_url": null,
    "path": "http://local.qipaoadmin/mall/connect-product",
    "per_page": 10,
    "prev_page_url": null,
    "to": 2,
    "total": 2
    }
    }
     */
    public function connectList(Request $request)
    {
        $param = $request->only('category_id', 'product_name', 'price', 'intimacy', 'sale_status', 'add_time');
        //参数校验
        $v = app('validator')->make($param, [
            'category_id' => 'integer',
            'product_name' => 'string|max:10',
            'price' => 'integer|min:1',
            'intimacy' => 'integer|min:0',
            'sale_status' => 'integer|in:-1,0,1',
            'add_time' => 'date_format:Y-m-d',
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $categoryId = $param['category_id'] ?? 0;
        $productName = $param['product_name'] ?? '';
        $price = $param['price'] ?? 0;
        $intimacy = $param['intimacy'] ?? 0;
        $saleStatus = $param['sale_status'] ?? -1;
        $addTime = $param['add_time'] ?? '';
        $pageSize = $param['page_size'] ?? 10;

        $where = [];
        if ($productName) {
            $where[] = ['p.title', '=', $productName];
        }
        if ($categoryId > 0) {
            $where[] = ['p.category_id', '=', $categoryId];
        }
        if ($price > 0) {
            $where[] = ['pp.original_price', '=', $price];
        }
        if ($intimacy) {
            $where[] = ['pp.intimacy', '=', $intimacy];
        }
        if ($saleStatus == 1) {
            $where[] = ['p.on_sale', '=', 1];
        } else if ($saleStatus == 0) {
            $where[] = ['p.on_sale', '=', 2];
        }
        if ($addTime) {
            $where[] = ['p.add_time', '>=', strtotime($addTime . ' 0:0:0')];
            $where[] = ['p.add_time', '<=', strtotime($addTime . ' 23:59:59')];
        }
        $where[] = ['p.module', '=', MallEnum::PRODUCT_MODULE_CONNECT];

        $list = app('model_mall_products')->from('ln_mall_products as p')
            ->leftJoin('ln_mall_products_price as pp', 'p.id', '=', 'pp.product_id')
            ->select('p.id', 'p.category_id', 'p.title as product_name', 'pp.day', 'pp.original_price', 'pp.intimacy', 'p.picture', 'p.special', 'show_type', 'p.on_sale', 'p.add_time')
            ->where($where)
            ->paginate($pageSize)->toArray();
        $categoryIds = array_column($list['data'], 'category_id');
        $categorys = app('model_mall_categories')->whereIn('id', $categoryIds)->pluck('title', 'id');

        foreach ($list['data'] as &$v) {
            if ($v['on_sale'] == 2) {
                $v['on_sale'] = 0;
            }
            $v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
            if (isset($categorys[$v['category_id']])) {
                $v['category_name'] = $categorys[$v['category_id']];
            }
        }
        return Response::sendData($list);
    }

    /**
     * @api {get} /mall/product/all-simple 获取所有商品，简单数据版
     *
     * @apiGroup 个性商城-单品管理
     *
     * @apiParam {string} product_name 商品名称，模糊搜索
     * @apiParam {int} sale_status 销售状态：-1（默认）：不限制；1：上架；2：下架
     * @apiParam {int} is_discount 是否折扣商品：-1（默认）：不限制；1：是；0：否
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": [
    {
    "id": 1,
    "title": "王爵",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/d0389654a358e777403a27e6d17ab823jaGzD.png",
    "special": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/e95546ca5dbfac041485872cf58924833KQs0.webp",
    }
    ]
    }
     */
    public function allSimple(Request $request)
    {
        $param = $request->only('product_name', 'sale_status', 'is_discount');
        //参数校验
        $v = app('validator')->make($param, [
            'product_name' => 'string|max:15',
            'sale_status' => 'integer|in:-1,1,2',
            'is_discount' => 'integer|in:-1,1,0',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $productName = $param['product_name'] ?? '';
        $saleStatus = $param['sale_status'] ?? -1;
        $isDiscount = $param['is_discount'] ?? -1;

        $where = [];
        if ($productName) {
            $where[] = ['title', 'like', "%$productName%"];
        }
        if ($saleStatus != -1) {
            $where = ['on_sale' => $saleStatus];
        }
        if ($isDiscount == 1) {
            $where[] = ['discount_rate', '<', 100];
        } else if ($isDiscount == 0) {
            $where[] = ['discount_rate', '=', 100];
        }
        $list = app('model_mall_products')
            ->select('id', 'title', 'picture', 'special')
            ->where($where)
            ->get();
        return Response::sendData($list);
    }
}
